package com.fashion.model;

import com.fashion.utils.StringUtils;
import com.fashion.utils.UrlKeyword;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: juventus
 * Date: 11-5-30
 * Time: 涓婂崍12:44
 */
@Entity
@Table(name = "NEWS")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class News implements Serializable {
    private String id;
    private Date createDate;
    private String introduction;
    private String context;
    private String title;
    private String viceTitle;
    private String indexTitle;
    private String author;
    private String briefImage;
    private String briefImageTitle;
    private boolean briefImageVertical;  //鏄惁绔栧浘
    private boolean hasComment;
    private String keywords;
    private Date publishDate;
    private int inIndex;//缃《绾у埆    2涓洪椤碉紝1涓洪閬撻〉锛?涓轰簩绾у強浠ヤ笂绾у埆棰戦亾椤?
    private Date indexStartDate;
    private Date indexFinishDate;
    private String metaData;
    private String seoTitle;
    private int viewTimes; //鏌ョ湅娆℃暟
    private String redirect;  //璺宠浆url
    private int indexLevel; //鎺ㄨ崘绾у埆
    private String source;  //鏉ユ簮
    private String sourceUrl;  //鏉ユ簮
    private String fullTitle;  //鏂囩珷鐨勫叏鏍囬锛屽仛alt灞炴€ф樉绀?
    private int viceTitlePosition; //鍓爣棰樹綅缃紝0涓烘鏍囬涓婃柟锛?涓轰笅鏂?
    private List<UrlKeyword> urlKeywords;
    private String videoId;
    private String creatorId;
    private boolean suggest; //鏄惁鎺ㄨ崘鏂伴椈
    private boolean published; //鏄惁宸插彂甯?

/*    @OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL}, mappedBy = "news")
@JoinColumn(name = "news_id", nullable = false)
private Set<NewsColumnItem> newsColumnItems = new HashSet<NewsColumnItem>();*/

    /*   public void clearNewsColumnItem(){
         newsColumnItems.clear();
    }

    public void addNewsColumnItem(NewsColumnItem newsColumnItem) {
        newsColumnItems.add(newsColumnItem);
    }*/

    public News() {
    }

    //Use this to save news to html file
    public News(String id) {
        this.id = id;
    }


    //鍚庡彴鏂伴椈绠＄悊椤甸潰浣跨敤
    public News(String id, String title, String indexTitle, String keywords, Date publishDate, Date createDate,int indexLevel, int inIndex, boolean published) {
        this.id = id;
        this.title = title;
        this.keywords = keywords;
        this.publishDate = publishDate;
        this.indexLevel = indexLevel;
        this.inIndex = inIndex;
        this.indexTitle = indexTitle;
        this.published = published;
        this.createDate = createDate;
    }

    public News(String id, String title, String indexTitle, String keywords, Date publishDate, int indexLevel, int inIndex, boolean published) {
        this.id = id;
        this.title = title;
        this.keywords = keywords;
        this.publishDate = publishDate;
        this.indexLevel = indexLevel;
        this.inIndex = inIndex;
        this.indexTitle = indexTitle;
        this.published = published;
    }

    public News(String id, String title, String keywords, Date createDate, int indexLevel, int inIndex) {
        this.id = id;
        this.title = title;
        this.keywords = keywords;
        this.createDate = createDate;
        this.indexLevel = indexLevel;
        this.inIndex = inIndex;
    }

    public News(String id, String title, String keywords, Date createDate) {
        this.id = id;
        this.title = title;
        this.keywords = keywords;
        this.createDate = createDate;
    }

    //棣栭〉鎴栨爮鐩〉闈娇鐢?
    public News(String id, String title, String indexTitle, String keywords, Date publishDate, String briefImage, String briefImageTitle, String introduction, boolean vertical) {
        this.id = id;
        this.title = title;
        this.keywords = keywords;
        this.publishDate = publishDate;
        this.briefImage = briefImage;
        this.briefImageTitle = briefImageTitle;
        this.introduction = introduction;
        this.briefImageVertical = vertical;
        this.indexTitle = indexTitle;
    }

    //Used for index html
    public News(String id, String title, String indexTitle, String keywords, Date publishDate, String briefImage, String briefImageTitle, String introduction, boolean vertical,
                int inIndex, int indexLevel) {
        this.id = id;
        this.title = title;
        this.keywords = keywords;
        this.publishDate = publishDate;
        this.briefImage = briefImage;
        this.briefImageTitle = briefImageTitle;
        this.introduction = introduction;
        this.briefImageVertical = vertical;
        this.indexTitle = indexTitle;
        this.inIndex = inIndex;
        this.indexLevel = indexLevel;
    }


    public News(String id, String title, String indexTitle, String keywords, Date publishDate, String briefImage, String briefImageTitle, String introduction) {
        this.id = id;
        this.title = title;
        this.indexTitle = indexTitle;
        this.keywords = keywords;
        this.publishDate = publishDate;
        this.briefImage = briefImage;
        this.briefImageTitle = briefImageTitle;
        this.introduction = introduction;
    }

    public News(String id, String title, String keywords, Date createDate, String briefImage, String briefImageTitle, String introduction, boolean vertical,String author, String source) {
        this.id = id;
        this.title = title;
        this.keywords = keywords;
        this.createDate = createDate;
        this.briefImage = briefImage;
        this.briefImageTitle = briefImageTitle;
        this.introduction = introduction;
        this.briefImageVertical = vertical;
        this.author = author;
        this.source = source;
    }
    
    public News(String id, String indexTitle, String title, String keywords, Date createDate, String briefImage, String briefImageTitle, String introduction, boolean briefImageVertical,String author, String source) {
        this.id = id;
        this.indexTitle = indexTitle;
        this.title = title;
        this.keywords = keywords;
        this.createDate = createDate;
        this.briefImage = briefImage;
        this.briefImageTitle = briefImageTitle;
        this.introduction = introduction;
        this.briefImageVertical = briefImageVertical;
        this.author = author;
        this.source = source;
    }
    
    public News(String id, String title, String keywords, Date publishDate, String briefImage, String briefImageTitle, String introduction) {
        this.id = id;
        this.title = title;
        this.keywords = keywords;
        this.publishDate = publishDate;
        this.briefImage = briefImage;
        this.briefImageTitle = briefImageTitle;
        this.introduction = introduction;
    }

    @Id
    @Column(name = "Id", unique = true, nullable = false, length = 64)
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Column(name = "createDateTime")
    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    @Column(name = "introduction")
    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }

    @Column(name = "context")
    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }

    @Column(name = "viceTitle")
    public String getViceTitle() {
        return viceTitle;
    }

    public void setViceTitle(String viceTitle) {
        this.viceTitle = viceTitle;
    }

    @Column(name = "briefImage")
    public String getBriefImage() {
        return briefImage;
    }

    public void setBriefImage(String briefImage) {
        this.briefImage = briefImage;
    }

    @Column(name = "hasComment")
    public boolean isHasComment() {
        return hasComment;
    }

    public void setHasComment(boolean hasComment) {
        this.hasComment = hasComment;
    }

    @Column(name = "keywords")
    public String getKeywords() {
        return keywords;
    }

    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }

    @Column(name = "publishDate")
    public Date getPublishDate() {
        return publishDate;
    }

    public void setPublishDate(Date publishDate) {
        this.publishDate = publishDate;
    }

    @Column(name = "indexStartDate")
    public Date getIndexStartDate() {
        return indexStartDate;
    }

    public void setIndexStartDate(Date indexStartDate) {
        this.indexStartDate = indexStartDate;
    }

    @Column(name = "indexFinishDate")
    public Date getIndexFinishDate() {
        return indexFinishDate;
    }

    public void setIndexFinishDate(Date indexFinishDate) {
        this.indexFinishDate = indexFinishDate;
    }

    @Column(name = "metaData")
    public String getMetaData() {
        return metaData;
    }

    public void setMetaData(String metaData) {
        this.metaData = metaData;
    }

    @Column(name = "seoTitle")
    public String getSeoTitle() {
        return seoTitle;
    }

    public void setSeoTitle(String seoTitle) {
        this.seoTitle = seoTitle;
    }

    @Column(name = "title")
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
/*
    public Set<NewsColumnItem> getNewsColumnItems() {
        return newsColumnItems;
    }

    public void setNewsColumnItems(Set<NewsColumnItem> newsColumnItems) {
        this.newsColumnItems = newsColumnItems;
    }*/

    @Column(name = "indexTitle")
    public String getIndexTitle() {
        return indexTitle;
    }

    public void setIndexTitle(String indexTitle) {
        this.indexTitle = indexTitle;
    }

    @Column(name = "author")
    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Column(name = "briefImageTitle")
    public String getBriefImageTitle() {
        return briefImageTitle;
    }

    public void setBriefImageTitle(String briefImageTitle) {
        this.briefImageTitle = briefImageTitle;
    }

    @Column(name = "inIndex")
    public int getInIndex() {
        return inIndex;
    }

    public void setInIndex(int inIndex) {
        this.inIndex = inIndex;
    }

    @Column(name = "viewTimes")
    public int getViewTimes() {
        return viewTimes;
    }

    public void setViewTimes(int viewTimes) {
        this.viewTimes = viewTimes;
    }

    @Column(name = "redirect")
    public String getRedirect() {
        return redirect;
    }

    public void setRedirect(String redirect) {
        this.redirect = redirect;
    }

    @Column(name = "indexLevel")
    public int getIndexLevel() {
        return indexLevel;
    }

    @Column()
    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public void setIndexLevel(int indexLevel) {
        this.indexLevel = indexLevel;
    }

    @Column()
    public boolean isBriefImageVertical() {
        return briefImageVertical;
    }

    public void setBriefImageVertical(boolean briefImageVertical) {
        this.briefImageVertical = briefImageVertical;
    }

    @Column
    public int getViceTitlePosition() {
        return viceTitlePosition;
    }

    public void setViceTitlePosition(int viceTitlePosition) {
        this.viceTitlePosition = viceTitlePosition;
    }

    @Column
    public String getVideoId() {
        return videoId;
    }

    public void setVideoId(String videoId) {
        this.videoId = videoId;
    }

    @Column
    public String getCreatorId() {
        return creatorId;
    }

    public void setCreatorId(String creatorId) {
        this.creatorId = creatorId;
    }

    @Column
    public boolean isSuggest() {
        return suggest;
    }

    public void setSuggest(boolean suggest) {
        this.suggest = suggest;
    }

    @Column
    public boolean isPublished() {
        return published;
    }

    public void setPublished(boolean published) {
        this.published = published;
    }

    @Column
    public String getSourceUrl() {
        return sourceUrl;
    }

    public void setSourceUrl(String sourceUrl) {
        this.sourceUrl = sourceUrl;
    }

    @Transient
    public List<UrlKeyword> getKeywordList() throws UnsupportedEncodingException {
        if (StringUtils.isNullOrEmpty(keywords)) {
            return new ArrayList<UrlKeyword>();
        }

        if(null != urlKeywords){
            return urlKeywords;
        }

        urlKeywords = new ArrayList<UrlKeyword>();
        List<String> rawKeywords = StringUtils.splitToList(keywords, " ");
        for (String keyword : rawKeywords) {
            String keywordWithoutSpace = keyword.replace("$", " ");
            /*urlKeywords.add(new UrlKeyword(keywordWithoutSpace, URLEncoder.encode(keywordWithoutSpace, "UTF-8")));*/
            urlKeywords.add(StringUtils.makeUrlKeyword(keywordWithoutSpace));
        }

        return urlKeywords;
    }

    @Transient
    public String getFullTitle() {
        return fullTitle;
    }

    public void setFullTitle(String fullTitle) {
        this.fullTitle = fullTitle;
    }
}
